package sketch.specs;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SketchedExhaustBlockTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(SketchedExhaustBlockTest.class);
	}
	
	public void testExhaustBlockParsing() {
		System.out.println("Testing testExhaustBlockParsing");
//		String filePath = "./tests/sketch/specs/exhaustblock.txt";;
		String filePath = "./testfiles/sketch/specs/ExhaustBlock.java";;
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> methods = creator.createSketchedMethodList();
		assertTrue(methods.size() == 1);
		
		Map<Comment, SketchedBlock> sketchedBlocks = creator.getSketchedBlocks();
		System.out.println(sketchedBlocks.size());
		assertTrue(sketchedBlocks.size() == 1);
		
		assertTrue(findExhaustBlock(sketchedBlocks.values()).size() == 1);
	}
	
	public void testExhaustBlockExpansion() {
		System.out.println("Testing testExhaustBlockExpansion");
		String filePath = "./testfiles/sketch/specs/ExhaustBlock.java";;
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> methods = creator.createSketchedMethodList();
		assertTrue(methods.size() == 1);
		
		Map<Comment, SketchedBlock> sketchedBlocks = creator.getSketchedBlocks();
		Collection<SketchedBlock> exhaustBlocks = findExhaustBlock(sketchedBlocks.values());
		
		assertTrue(exhaustBlocks.size() == 1);
		
		//see the expansion result
		for(SketchedBlock exhaustBlock : exhaustBlocks) {
			List<Block> blocks = exhaustBlock.expandAll();
			for(Block b : blocks) {
				System.out.println(b);
			}
			assertEquals(4 + 4*4 + 4*4*4 + 4*4*4*4, blocks.size());
		}
	}
	
	//a utility method
	private Collection<SketchedBlock> findExhaustBlock(Collection<SketchedBlock> blocks) {
		Collection<SketchedBlock> retBlocks = new LinkedList<SketchedBlock>();
		
		for(SketchedBlock block : blocks) {
			if(block instanceof SketchedExhaustBlock) {
				retBlocks.add(block);
			}
		}
		
		return retBlocks;
	}
}
